﻿@page "/order"
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Components.QuickGrid
@using BlazorAppAuth.Models
@using BlazorAppAuth.Data
@inject IDbContextFactory<BlazorAppAuth.Data.BlazorAppAuthContext> DbFactory
@implements IAsyncDisposable
@rendermode InteractiveServer

<h3>订单操作</h3>

<BlazorAppAuth.Components.Pages.OrderPages.Create OnOrderCreated="HandleButtonClick" />


<QuickGrid Items="FilterableOrder" data-toggle="table" Class="table" @ref="myGrid" id="dataTable">
    <PropertyColumn Property="@(o => o.Id)" Title="ID" />
    <PropertyColumn Property="@(o => o.OrderId)" Title="订单编号" />
    <PropertyColumn Property="@(o => o.OrderType)" Title="订单类型" />
    <PropertyColumn Property="@(o => o.UserType)" Title="用户类型" />
    <PropertyColumn Property="@(o => o.UserName)" Title="用户名" />
    <PropertyColumn Property="@(o => o.StartTime)" Title="启动时间" />
    <PropertyColumn Property="@(o => o.OrderTime)" Title="下单时间" />
    <PropertyColumn Property="@(o => o.Quantity)" Title="数量" />
    <PropertyColumn Property="@(o => o.Priority)" Title="优先级" />
    <PropertyColumn Property="@(o => o.AssemblyTime)" Title="装配时间" />
    <PropertyColumn Property="@(o => o.DrillingTime)" Title="钻孔时间" />
    <PropertyColumn Property="@(o => o.AssemblyCount)" Title="装配次数" />
    <PropertyColumn Property="@(o => o.DrillingCount)" Title="钻孔次数" />
    <PropertyColumn Property="@(o => o.OrderStatus)" Title="订单状态" />
    <PropertyColumn Property="@(o => o.ElectricityConsumption)" Title="订单用电量（w·h）" />
    <PropertyColumn Property="@(o => o.GasConsumption)" Title="订单用气量（L）" />
    <TemplateColumn Context="order" Align=Align.Right Title="操作">
        <button class="btn btn-danger btn-sm ms-1" @onclick="()=> DeleteOrder(order)">删除</button>
    </TemplateColumn>
</QuickGrid>
<style>

    #dataTable tbody tr:hover {
      background-color: #198754;
      cursor: move;
    }

</style>
<script>

    $(document).ready(function () {
        $(".quickgrid.table tbody").sortable({
            helper: function (e, ui) {
                ui.children().each(function () {
                    $(this).width($(this).width());
                });
                return ui;
            },
            axis: "y"
        }).disableSelection();
    });

</script>


@code {
    BlazorAppAuthContext context = default!;

    private IQueryable<Models.Order> FilterableOrder = default!;


    private QuickGrid<Models.Order> myGrid;

    protected override void OnInitialized()
    {
        context = DbFactory.CreateDbContext();
        FilterableOrder = context.Order.Where(o => o.OrderStatus.Contains(Constract.Constracts.OrderStatusType.待下发.ToString()));
    }

    private async Task HandleButtonClick()
    {
        await js.InvokeVoidAsync("toggle_dialog", "下单成功");
        await myGrid.RefreshDataAsync();
    }


    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }
    public async Task CheckLogin()
    {
        var authState = await authenticationStateTask;
        var user = authState.User;

        if (!user.Identity.IsAuthenticated)
        {
            NavigationManager.NavigateTo("/login");
        }

    }

    protected async void DeleteOrder(Models.Order o)
    {
        bool confrmDel = await js.InvokeAsync<bool>("confirm", "确定要删除吗?");
        if (confrmDel)
        {
            using var context = DbFactory.CreateDbContext();
            context.Order.Remove(o);
            await context.SaveChangesAsync();
            await js.InvokeVoidAsync("toggle_dialog", "删除成功");
            await myGrid.RefreshDataAsync();
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await CheckLogin();
    }

    public async ValueTask DisposeAsync() => await context.DisposeAsync();
}
